```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_knit$set(root.dir = 'C:/Users/Admin/Documents/Learning Python Folder1/Python Essence Training/Fantasy-Baseball/Data')
options(knitr.table.format = "html") 
options(digits=2)
options(scipen = 100)
```


#### [Go back to the main page](https://dissipation.github.io/DarshDSPortfolio/)


All data is from [FanGraphs.](https://www.fangraphs.com/) I have no affiliation with FanGraphs, but please consider contributing to their [website](https://plus.fangraphs.com/shop/) if you found this project informative.    

Positional Scarcity is determined by [ATC WAR Projections](https://www.fangraphs.com/projections.aspx?pos=all&stats=bat&type=atc&team=0&lg=all&players=0) from 3-31-2022.  

***    

# **WAR PROJECTIONS AND MODEL SCORES** {.tabset .tabset-pills}   
Players that have an `NA` for `Model_Score` did not play in the MLB in 2021 and have a negative or zero Position Rank.  
```{r}
### Fill in code
```


***    

## Overall  {.tabset .tabset-pills}    
My model scores for a player are `Model_Score`. *Note:Pitching Scores are normalized to equal the sum of WAR for hitters so they have inflated model scores. Other tabs have non-normalized scores.*  
```{r}
## Ranks above ADP should be for total


hitting_Data_NonAdj_Projections_Total = sqldf(
  "
  select a.*, b.Predict_Score
  from
  lookuptable a
  left join
  hitting_Data_NonAdj_Projections5xb b
  on a.playerid = b.playerid
  "
)

FanGraph_Positions_Batter_Total =
hitting_Data_NonAdj_Projections_Total %>% 
  mutate(
    Avg_PA = mean(PA),
    AdjPredict_Score_raw = ifelse(is.na(Predict_Score),NA,Predict_Score*(PA/Avg_PA))
  ) %>% select(-Avg_PA)



pitching_Data_NonAdj_Projections_Total = sqldf(
  "
  select a.*, b.Predict_Score
  from
  lookuptable2 a
  left join
  Pitching_Data_NonAdj_Projections5x b
  on a.playerid = b.playerid
  "
)

Adjustment_factor= (sum(FanGraph_Positions_Batter_Total$WAR,na.rm = T))/(sum(pitching_Data_NonAdj_Projections_Total$WAR,na.rm = T))


FanGraph_Positions_Pitcher_Total =
pitching_Data_NonAdj_Projections_Total %>% 
  mutate(
    Avg_IP = mean(IP),
    AdjPredict_Score_raw = ifelse(is.na(Predict_Score),NA,Predict_Score*(IP/Avg_IP)*Adjustment_factor)
  ) %>% select(-Avg_IP)


Pitching_for_Overall = FanGraph_Positions_Pitcher_Total %>% select(Name,Team,Position,ADP,WAR,AdjPredict_Score_raw,Positional_Scarcity)
  
  
Hitting_for_Overall =  FanGraph_Positions_Batter_Total %>% select(Name,Team,Position,ADP,WAR,AdjPredict_Score_raw,Positional_Scarcity)


FanGraph_Overall1 = rbind(Pitching_for_Overall,Hitting_for_Overall)


```

```{r}


FanGraph_Overall =
FanGraph_Overall1 %>% 
  mutate(
    WAR_rank = order(order(rank(WAR,ties.method = 'average'),decreasing = TRUE)),
    AdjPredict_Score = AdjPredict_Score_raw,
    AdjPredict_Score_Rank = order(order(rank(AdjPredict_Score_raw,ties.method = 'average'),decreasing = TRUE))-sum(is.na(AdjPredict_Score_raw)),
        Ranks_Above_ADP = ADP - AdjPredict_Score_Rank
  ) %>%
  select (Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity)


ft_dt <- FanGraph_Overall[1:nrow(FanGraph_Overall), 1:ncol(FanGraph_Overall)] %>% 
 arrange((ADP))

ft_dt$ADP <- color_tile("white", "red")(ft_dt$ADP)

ft_dt$WAR <- color_bar("lightblue")(ft_dt$WAR)


ft_dt$AdjPredict_Score<-  round((ft_dt$AdjPredict_Score),2)

ft_dt$Model_Score<- color_bar("lightblue")(ft_dt$AdjPredict_Score)

ft_dt$WAR_Rank <- color_tile("green","orange")(ft_dt$WAR_rank)

ft_dt$Model_Rank <- color_tile("green","orange")(ft_dt$AdjPredict_Score_Rank) 


ft_dt$Ranks_Above_ADP <- 
  ifelse(
  ft_dt$Ranks_Above_ADP < 0,
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "red", italic = T),
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "green", italic = T)
)

ft_dt$Positional_Scarcity <- round(ft_dt$Positional_Scarcity, 2)

ft_dt$Pos_Scarcity <- color_tile("red","white")(ft_dt$Positional_Scarcity)

#Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity

ft_dt2 <- ft_dt[c("Name","Team","Position","ADP", "WAR", "Model_Score", "WAR_Rank","Pos_Scarcity")]


table_export = 
kbl(ft_dt2, escape = F) %>% 
 kable_material(c("striped", "hover","condensed","responsive"),full_width = F,fixed_thead = T) %>%   column_spec(6, width = "3cm")# %>%
 # add_header_above(c(" ", "Scores" = 5, "Ranks" = 3," "))

table_export  

```


## Hitters by Position {.tabset .tabset-pills}
My non-normalized model scores for a player are `Model_Score`.  

### Overall

```{r}
FanGraph_Positions_Batter_All_POS =
FanGraph_Positions_Batter_Total %>% 
  mutate(
    max_predscore= max(AdjPredict_Score_raw,na.rm = T),
    AdjPredict_Score = ifelse (is.na(AdjPredict_Score_raw),NA,AdjPredict_Score_raw),
    WAR_rank = order(order(rank(WAR,ties.method = 'average'),decreasing = TRUE)),
    AdjPredict_Score_Rank = order(order(rank(AdjPredict_Score,ties.method = 'average'),decreasing = TRUE))-sum(is.na(AdjPredict_Score)),
        Ranks_Above_ADP = ADP - AdjPredict_Score_Rank
  ) %>%
filter(PA > 1) %>%   
  select (Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity)

ft_dt <- FanGraph_Positions_Batter_All_POS[1:nrow(FanGraph_Positions_Batter_All_POS), 1:ncol(FanGraph_Positions_Batter_All_POS)] %>% 
#%>% 
#filter(AdjPredict_Score_Rank>0)
 arrange((ADP))

ft_dt$ADP <- color_tile("white", "red")(ft_dt$ADP)

ft_dt$WAR <- color_bar("lightblue")(ft_dt$WAR)


ft_dt$AdjPredict_Score<-  round((ft_dt$AdjPredict_Score),2)

ft_dt$Model_Score<- color_bar("lightblue")(ft_dt$AdjPredict_Score)

ft_dt$WAR_Rank <- color_tile("green","orange")(ft_dt$WAR_rank)

ft_dt$Hitting_Rank <- color_tile("green","orange")(ft_dt$AdjPredict_Score_Rank) 


ft_dt$Ranks_Above_ADP <- 
  ifelse(
  ft_dt$Ranks_Above_ADP < 0,
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "red", italic = T),
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "green", italic = T)
)

ft_dt$Positional_Scarcity <- round(ft_dt$Positional_Scarcity, 2)

ft_dt$Pos_Scarcity <- color_tile("red","white")(ft_dt$Positional_Scarcity)

#Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity

ft_dt2 <- ft_dt[c("Name","Team","Position","ADP", "WAR", "Model_Score", "WAR_Rank","Hitting_Rank","Pos_Scarcity")]


table_export = 
kbl(ft_dt2, escape = F) %>% 
 kable_material(c("striped", "hover","condensed","responsive"),full_width = F,fixed_thead = T) %>%   column_spec(6, width = "3cm")# %>%
 # add_header_above(c(" ", "Scores" = 5, "Ranks" = 3," "))

table_export  

```


### 1B
WAR Ranks are for the position, and Pos_Rank is based on Model Score  , and Pos_Rank is based on Model Score    
```{r}


FanGraph_Positions_Batter_All_POS =
FanGraph_Positions_Batter_Total %>%
filter(Position1b == 1) %>% 
  mutate(
    max_predscore= max(AdjPredict_Score_raw,na.rm = T),
    AdjPredict_Score = ifelse (is.na(AdjPredict_Score_raw),NA,AdjPredict_Score_raw),
    WAR_rank = order(order(rank(WAR,ties.method = 'average'),decreasing = TRUE)),
    AdjPredict_Score_Rank = order(order(rank(AdjPredict_Score,ties.method = 'average'),decreasing = TRUE))-sum(is.na(AdjPredict_Score)),
        Ranks_Above_ADP = ADP - AdjPredict_Score_Rank
  ) %>%   
  select (Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity)


ft_dt <- FanGraph_Positions_Batter_All_POS[1:nrow(FanGraph_Positions_Batter_All_POS), 1:ncol(FanGraph_Positions_Batter_All_POS)] %>% 
#%>% 
#filter(AdjPredict_Score_Rank>0)
 arrange((ADP))

ft_dt$ADP <- color_tile("white", "red")(ft_dt$ADP)

ft_dt$WAR <- color_bar("lightblue")(ft_dt$WAR)


ft_dt$AdjPredict_Score<-  round((ft_dt$AdjPredict_Score),2)

ft_dt$Model_Score<- color_bar("lightblue")(ft_dt$AdjPredict_Score)

ft_dt$WAR_Rank <- color_tile("green","orange")(ft_dt$WAR_rank)

ft_dt$Position_Rank <- color_tile("green","orange")(ft_dt$AdjPredict_Score_Rank) 

ft_dt$Ranks_Above_ADP <- 
  ifelse(
  ft_dt$Ranks_Above_ADP < 0,
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "red", italic = T),
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "green", italic = T)
)

ft_dt$Positional_Scarcity <- round(ft_dt$Positional_Scarcity, 2)

ft_dt$Pos_Scarcity <- color_tile("red","white")(ft_dt$Positional_Scarcity)

#Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity

ft_dt2 <- ft_dt[c("Name","Team","Position","ADP", "WAR", "Model_Score", "WAR_Rank","Position_Rank","Pos_Scarcity")]


table_export = 
kbl(ft_dt2, escape = F) %>% 
 kable_material(c("striped", "hover","condensed","responsive"),full_width = F,fixed_thead = T) %>%   column_spec(6, width = "3cm")# %>%
 # add_header_above(c(" ", "Scores" = 5, "Ranks" = 3," "))

table_export  
```


### 2B    
WAR Ranks are for the position, and Pos_Rank is based on Model Score   
```{r}

FanGraph_Positions_Batter_All_POS =
FanGraph_Positions_Batter_Total %>%
filter(Position2b == 1) %>% 
  mutate(
    max_predscore= max(AdjPredict_Score_raw,na.rm = T),
    AdjPredict_Score = ifelse (is.na(AdjPredict_Score_raw),NA,AdjPredict_Score_raw),
    WAR_rank = order(order(rank(WAR,ties.method = 'average'),decreasing = TRUE)),
    AdjPredict_Score_Rank = order(order(rank(AdjPredict_Score,ties.method = 'average'),decreasing = TRUE))-sum(is.na(AdjPredict_Score)),
        Ranks_Above_ADP = ADP - AdjPredict_Score_Rank
  ) %>%   
  select (Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity)


ft_dt <- FanGraph_Positions_Batter_All_POS[1:nrow(FanGraph_Positions_Batter_All_POS), 1:ncol(FanGraph_Positions_Batter_All_POS)] %>% 
#%>% 
#filter(AdjPredict_Score_Rank>0)
 arrange((ADP))

ft_dt$ADP <- color_tile("white", "red")(ft_dt$ADP)

ft_dt$WAR <- color_bar("lightblue")(ft_dt$WAR)


ft_dt$AdjPredict_Score<-  round((ft_dt$AdjPredict_Score),2)

ft_dt$Model_Score<- color_bar("lightblue")(ft_dt$AdjPredict_Score)

ft_dt$WAR_Rank <- color_tile("green","orange")(ft_dt$WAR_rank)

ft_dt$Position_Rank <- color_tile("green","orange")(ft_dt$AdjPredict_Score_Rank) 

ft_dt$Ranks_Above_ADP <- 
  ifelse(
  ft_dt$Ranks_Above_ADP < 0,
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "red", italic = T),
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "green", italic = T)
)

ft_dt$Positional_Scarcity <- round(ft_dt$Positional_Scarcity, 2)

ft_dt$Pos_Scarcity <- color_tile("red","white")(ft_dt$Positional_Scarcity)

#Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity

ft_dt2 <- ft_dt[c("Name","Team","Position","ADP", "WAR", "Model_Score", "WAR_Rank","Position_Rank","Pos_Scarcity")]


table_export = 
kbl(ft_dt2, escape = F) %>% 
 kable_material(c("striped", "hover","condensed","responsive"),full_width = F,fixed_thead = T) %>%   column_spec(6, width = "3cm")# %>%
 # add_header_above(c(" ", "Scores" = 5, "Ranks" = 3," "))

table_export  
```

### 3B    
WAR Ranks are for the position, and Pos_Rank is based on Model Score      
```{r}

FanGraph_Positions_Batter_All_POS =
FanGraph_Positions_Batter_Total %>%
filter(Position3b == 1) %>% 
  mutate(
    max_predscore= max(AdjPredict_Score_raw,na.rm = T),
    AdjPredict_Score = ifelse (is.na(AdjPredict_Score_raw),NA,AdjPredict_Score_raw),
    WAR_rank = order(order(rank(WAR,ties.method = 'average'),decreasing = TRUE)),
    AdjPredict_Score_Rank = order(order(rank(AdjPredict_Score,ties.method = 'average'),decreasing = TRUE))-sum(is.na(AdjPredict_Score)),
        Ranks_Above_ADP = ADP - AdjPredict_Score_Rank
  ) %>%   
  select (Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity)


ft_dt <- FanGraph_Positions_Batter_All_POS[1:nrow(FanGraph_Positions_Batter_All_POS), 1:ncol(FanGraph_Positions_Batter_All_POS)] %>% 
#%>% 
#filter(AdjPredict_Score_Rank>0)
 arrange((ADP))

ft_dt$ADP <- color_tile("white", "red")(ft_dt$ADP)

ft_dt$WAR <- color_bar("lightblue")(ft_dt$WAR)


ft_dt$AdjPredict_Score<-  round((ft_dt$AdjPredict_Score),2)

ft_dt$Model_Score<- color_bar("lightblue")(ft_dt$AdjPredict_Score)

ft_dt$WAR_Rank <- color_tile("green","orange")(ft_dt$WAR_rank)

ft_dt$Position_Rank <- color_tile("green","orange")(ft_dt$AdjPredict_Score_Rank) 

ft_dt$Ranks_Above_ADP <- 
  ifelse(
  ft_dt$Ranks_Above_ADP < 0,
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "red", italic = T),
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "green", italic = T)
)

ft_dt$Positional_Scarcity <- round(ft_dt$Positional_Scarcity, 2)

ft_dt$Pos_Scarcity <- color_tile("red","white")(ft_dt$Positional_Scarcity)

#Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity

ft_dt2 <- ft_dt[c("Name","Team","Position","ADP", "WAR", "Model_Score", "WAR_Rank","Position_Rank","Pos_Scarcity")]


table_export = 
kbl(ft_dt2, escape = F) %>% 
 kable_material(c("striped", "hover","condensed","responsive"),full_width = F,fixed_thead = T) %>%   column_spec(6, width = "3cm")# %>%
 # add_header_above(c(" ", "Scores" = 5, "Ranks" = 3," "))

table_export  
```

### SS  
WAR Ranks are for the position, and Pos_Rank is based on Model Score    
```{r}

FanGraph_Positions_Batter_All_POS =
FanGraph_Positions_Batter_Total %>%
filter(PositionSS == 1) %>% 
  mutate(
    max_predscore= max(AdjPredict_Score_raw,na.rm = T),
    AdjPredict_Score = ifelse (is.na(AdjPredict_Score_raw),NA,AdjPredict_Score_raw),
    WAR_rank = order(order(rank(WAR,ties.method = 'average'),decreasing = TRUE)),
    AdjPredict_Score_Rank = order(order(rank(AdjPredict_Score,ties.method = 'average'),decreasing = TRUE))-sum(is.na(AdjPredict_Score)),
        Ranks_Above_ADP = ADP - AdjPredict_Score_Rank
  ) %>%   
  select (Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity)


ft_dt <- FanGraph_Positions_Batter_All_POS[1:nrow(FanGraph_Positions_Batter_All_POS), 1:ncol(FanGraph_Positions_Batter_All_POS)] %>% 
#%>% 
#filter(AdjPredict_Score_Rank>0)
 arrange((ADP))

ft_dt$ADP <- color_tile("white", "red")(ft_dt$ADP)

ft_dt$WAR <- color_bar("lightblue")(ft_dt$WAR)


ft_dt$AdjPredict_Score<-  round((ft_dt$AdjPredict_Score),2)

ft_dt$Model_Score<- color_bar("lightblue")(ft_dt$AdjPredict_Score)

ft_dt$WAR_Rank <- color_tile("green","orange")(ft_dt$WAR_rank)

ft_dt$Position_Rank <- color_tile("green","orange")(ft_dt$AdjPredict_Score_Rank) 

ft_dt$Ranks_Above_ADP <- 
  ifelse(
  ft_dt$Ranks_Above_ADP < 0,
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "red", italic = T),
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "green", italic = T)
)

ft_dt$Positional_Scarcity <- round(ft_dt$Positional_Scarcity, 2)

ft_dt$Pos_Scarcity <- color_tile("red","white")(ft_dt$Positional_Scarcity)

#Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity

ft_dt2 <- ft_dt[c("Name","Team","Position","ADP", "WAR", "Model_Score", "WAR_Rank","Position_Rank","Pos_Scarcity")]


table_export = 
kbl(ft_dt2, escape = F) %>% 
 kable_material(c("striped", "hover","condensed","responsive"),full_width = F,fixed_thead = T) %>%   column_spec(6, width = "3cm")# %>%
 # add_header_above(c(" ", "Scores" = 5, "Ranks" = 3," "))

table_export   
```

### LF  
WAR Ranks are for the position, and Pos_Rank is based on Model Score      
```{r}

FanGraph_Positions_Batter_All_POS =
FanGraph_Positions_Batter_Total %>%
filter(PositionLF == 1) %>% 
  mutate(
    max_predscore= max(AdjPredict_Score_raw,na.rm = T),
    AdjPredict_Score = ifelse (is.na(AdjPredict_Score_raw),NA,AdjPredict_Score_raw),
    WAR_rank = order(order(rank(WAR,ties.method = 'average'),decreasing = TRUE)),
    AdjPredict_Score_Rank = order(order(rank(AdjPredict_Score,ties.method = 'average'),decreasing = TRUE))-sum(is.na(AdjPredict_Score)),
        Ranks_Above_ADP = ADP - AdjPredict_Score_Rank
  ) %>%   
  select (Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity)


ft_dt <- FanGraph_Positions_Batter_All_POS[1:nrow(FanGraph_Positions_Batter_All_POS), 1:ncol(FanGraph_Positions_Batter_All_POS)] %>% 
#%>% 
#filter(AdjPredict_Score_Rank>0)
 arrange((ADP))

ft_dt$ADP <- color_tile("white", "red")(ft_dt$ADP)

ft_dt$WAR <- color_bar("lightblue")(ft_dt$WAR)


ft_dt$AdjPredict_Score<-  round((ft_dt$AdjPredict_Score),2)

ft_dt$Model_Score<- color_bar("lightblue")(ft_dt$AdjPredict_Score)

ft_dt$WAR_Rank <- color_tile("green","orange")(ft_dt$WAR_rank)

ft_dt$Position_Rank <- color_tile("green","orange")(ft_dt$AdjPredict_Score_Rank) 

ft_dt$Ranks_Above_ADP <- 
  ifelse(
  ft_dt$Ranks_Above_ADP < 0,
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "red", italic = T),
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "green", italic = T)
)

ft_dt$Positional_Scarcity <- round(ft_dt$Positional_Scarcity, 2)

ft_dt$Pos_Scarcity <- color_tile("red","white")(ft_dt$Positional_Scarcity)

#Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity

ft_dt2 <- ft_dt[c("Name","Team","Position","ADP", "WAR", "Model_Score", "WAR_Rank","Position_Rank","Pos_Scarcity")]


table_export = 
kbl(ft_dt2, escape = F) %>% 
 kable_material(c("striped", "hover","condensed","responsive"),full_width = F,fixed_thead = T) %>%   column_spec(6, width = "3cm")# %>%
 # add_header_above(c(" ", "Scores" = 5, "Ranks" = 3," "))

table_export  

```

### RF  
WAR Ranks are for the position, and Pos_Rank is based on Model Score      
```{r}

FanGraph_Positions_Batter_All_POS =
FanGraph_Positions_Batter_Total %>%
filter(PositionRF == 1) %>% 
  mutate(
    max_predscore= max(AdjPredict_Score_raw,na.rm = T),
    AdjPredict_Score = ifelse (is.na(AdjPredict_Score_raw),NA,AdjPredict_Score_raw),
    WAR_rank = order(order(rank(WAR,ties.method = 'average'),decreasing = TRUE)),
    AdjPredict_Score_Rank = order(order(rank(AdjPredict_Score,ties.method = 'average'),decreasing = TRUE))-sum(is.na(AdjPredict_Score)),
        Ranks_Above_ADP = ADP - AdjPredict_Score_Rank
  ) %>%   
  select (Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity)


ft_dt <- FanGraph_Positions_Batter_All_POS[1:nrow(FanGraph_Positions_Batter_All_POS), 1:ncol(FanGraph_Positions_Batter_All_POS)] %>% 
#%>% 
#filter(AdjPredict_Score_Rank>0)
 arrange((ADP))

ft_dt$ADP <- color_tile("white", "red")(ft_dt$ADP)

ft_dt$WAR <- color_bar("lightblue")(ft_dt$WAR)


ft_dt$AdjPredict_Score<-  round((ft_dt$AdjPredict_Score),2)

ft_dt$Model_Score<- color_bar("lightblue")(ft_dt$AdjPredict_Score)

ft_dt$WAR_Rank <- color_tile("green","orange")(ft_dt$WAR_rank)

ft_dt$Position_Rank <- color_tile("green","orange")(ft_dt$AdjPredict_Score_Rank) 

ft_dt$Ranks_Above_ADP <- 
  ifelse(
  ft_dt$Ranks_Above_ADP < 0,
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "red", italic = T),
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "green", italic = T)
)

ft_dt$Positional_Scarcity <- round(ft_dt$Positional_Scarcity, 2)

ft_dt$Pos_Scarcity <- color_tile("red","white")(ft_dt$Positional_Scarcity)

#Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity

ft_dt2 <- ft_dt[c("Name","Team","Position","ADP", "WAR", "Model_Score", "WAR_Rank","Position_Rank","Pos_Scarcity")]


table_export = 
kbl(ft_dt2, escape = F) %>% 
 kable_material(c("striped", "hover","condensed","responsive"),full_width = F,fixed_thead = T) %>%   column_spec(6, width = "3cm")# %>%
 # add_header_above(c(" ", "Scores" = 5, "Ranks" = 3," "))

table_export  

```

### CF   
WAR Ranks are for the position, and Pos_Rank is based on Model Score      
```{r}


FanGraph_Positions_Batter_All_POS =
FanGraph_Positions_Batter_Total %>%
filter(PositionCF == 1) %>% 
  mutate(
    max_predscore= max(AdjPredict_Score_raw,na.rm = T),
    AdjPredict_Score = ifelse (is.na(AdjPredict_Score_raw),NA,AdjPredict_Score_raw),
    WAR_rank = order(order(rank(WAR,ties.method = 'average'),decreasing = TRUE)),
    AdjPredict_Score_Rank = order(order(rank(AdjPredict_Score,ties.method = 'average'),decreasing = TRUE))-sum(is.na(AdjPredict_Score)),
        Ranks_Above_ADP = ADP - AdjPredict_Score_Rank
  ) %>%   
  select (Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity)


ft_dt <- FanGraph_Positions_Batter_All_POS[1:nrow(FanGraph_Positions_Batter_All_POS), 1:ncol(FanGraph_Positions_Batter_All_POS)] %>% 
#%>% 
#filter(AdjPredict_Score_Rank>0)
 arrange((ADP))

ft_dt$ADP <- color_tile("white", "red")(ft_dt$ADP)

ft_dt$WAR <- color_bar("lightblue")(ft_dt$WAR)


ft_dt$AdjPredict_Score<-  round((ft_dt$AdjPredict_Score),2)

ft_dt$Model_Score<- color_bar("lightblue")(ft_dt$AdjPredict_Score)

ft_dt$WAR_Rank <- color_tile("green","orange")(ft_dt$WAR_rank)

ft_dt$Position_Rank <- color_tile("green","orange")(ft_dt$AdjPredict_Score_Rank) 

ft_dt$Ranks_Above_ADP <- 
  ifelse(
  ft_dt$Ranks_Above_ADP < 0,
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "red", italic = T),
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "green", italic = T)
)

ft_dt$Positional_Scarcity <- round(ft_dt$Positional_Scarcity, 2)

ft_dt$Pos_Scarcity <- color_tile("red","white")(ft_dt$Positional_Scarcity)

#Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity

ft_dt2 <- ft_dt[c("Name","Team","Position","ADP", "WAR", "Model_Score", "WAR_Rank","Position_Rank","Pos_Scarcity")]


table_export = 
kbl(ft_dt2, escape = F) %>% 
 kable_material(c("striped", "hover","condensed","responsive"),full_width = F,fixed_thead = T) %>%   column_spec(6, width = "3cm")# %>%
 # add_header_above(c(" ", "Scores" = 5, "Ranks" = 3," "))

table_export  
```

### OF    
WAR Ranks are for the position, and Pos_Rank is based on Model Score     
```{r}


FanGraph_Positions_Batter_All_POS =
FanGraph_Positions_Batter_Total %>%
filter(PositionOF == 1) %>% 
  mutate(
    max_predscore= max(AdjPredict_Score_raw,na.rm = T),
    AdjPredict_Score = ifelse (is.na(AdjPredict_Score_raw),NA,AdjPredict_Score_raw),
    WAR_rank = order(order(rank(WAR,ties.method = 'average'),decreasing = TRUE)),
    AdjPredict_Score_Rank = order(order(rank(AdjPredict_Score,ties.method = 'average'),decreasing = TRUE))-sum(is.na(AdjPredict_Score)),
        Ranks_Above_ADP = ADP - AdjPredict_Score_Rank
  ) %>%   
  select (Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity)


ft_dt <- FanGraph_Positions_Batter_All_POS[1:nrow(FanGraph_Positions_Batter_All_POS), 1:ncol(FanGraph_Positions_Batter_All_POS)] %>% 
#%>% 
#filter(AdjPredict_Score_Rank>0)
 arrange((ADP))

ft_dt$ADP <- color_tile("white", "red")(ft_dt$ADP)

ft_dt$WAR <- color_bar("lightblue")(ft_dt$WAR)


ft_dt$AdjPredict_Score<-  round((ft_dt$AdjPredict_Score),2)

ft_dt$Model_Score<- color_bar("lightblue")(ft_dt$AdjPredict_Score)

ft_dt$WAR_Rank <- color_tile("green","orange")(ft_dt$WAR_rank)

ft_dt$Position_Rank <- color_tile("green","orange")(ft_dt$AdjPredict_Score_Rank) 

ft_dt$Ranks_Above_ADP <- 
  ifelse(
  ft_dt$Ranks_Above_ADP < 0,
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "red", italic = T),
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "green", italic = T)
)

ft_dt$Positional_Scarcity <- round(ft_dt$Positional_Scarcity, 2)

ft_dt$Pos_Scarcity <- color_tile("red","white")(ft_dt$Positional_Scarcity)

#Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity

ft_dt2 <- ft_dt[c("Name","Team","Position","ADP", "WAR", "Model_Score", "WAR_Rank","Position_Rank","Pos_Scarcity")]


table_export = 
kbl(ft_dt2, escape = F) %>% 
 kable_material(c("striped", "hover","condensed","responsive"),full_width = F,fixed_thead = T) %>%   column_spec(6, width = "3cm")# %>%
 # add_header_above(c(" ", "Scores" = 5, "Ranks" = 3," "))

table_export  
```

### C  
WAR Ranks are for the position, and Pos_Rank is based on Model Score      
```{r}

FanGraph_Positions_Batter_All_POS =
FanGraph_Positions_Batter_Total %>%
filter(PositionC == 1) %>% 
  mutate(
    max_predscore= max(AdjPredict_Score_raw,na.rm = T),
    AdjPredict_Score = ifelse (is.na(AdjPredict_Score_raw),NA,AdjPredict_Score_raw),
    WAR_rank = order(order(rank(WAR,ties.method = 'average'),decreasing = TRUE)),
    AdjPredict_Score_Rank = order(order(rank(AdjPredict_Score,ties.method = 'average'),decreasing = TRUE))-sum(is.na(AdjPredict_Score)),
        Ranks_Above_ADP = ADP - AdjPredict_Score_Rank
  ) %>%   
  select (Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity)


ft_dt <- FanGraph_Positions_Batter_All_POS[1:nrow(FanGraph_Positions_Batter_All_POS), 1:ncol(FanGraph_Positions_Batter_All_POS)] %>% 
#%>% 
#filter(AdjPredict_Score_Rank>0)
 arrange((ADP))

ft_dt$ADP <- color_tile("white", "red")(ft_dt$ADP)

ft_dt$WAR <- color_bar("lightblue")(ft_dt$WAR)


ft_dt$AdjPredict_Score<-  round((ft_dt$AdjPredict_Score),2)

ft_dt$Model_Score<- color_bar("lightblue")(ft_dt$AdjPredict_Score)

ft_dt$WAR_Rank <- color_tile("green","orange")(ft_dt$WAR_rank)

ft_dt$Position_Rank <- color_tile("green","orange")(ft_dt$AdjPredict_Score_Rank) 

ft_dt$Ranks_Above_ADP <- 
  ifelse(
  ft_dt$Ranks_Above_ADP < 0,
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "red", italic = T),
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "green", italic = T)
)

ft_dt$Positional_Scarcity <- round(ft_dt$Positional_Scarcity, 2)

ft_dt$Pos_Scarcity <- color_tile("red","white")(ft_dt$Positional_Scarcity)

#Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity

ft_dt2 <- ft_dt[c("Name","Team","Position","ADP", "WAR", "Model_Score", "WAR_Rank","Position_Rank","Pos_Scarcity")]


table_export = 
kbl(ft_dt2, escape = F) %>% 
 kable_material(c("striped", "hover","condensed","responsive"),full_width = F,fixed_thead = T) %>%   column_spec(6, width = "3cm")# %>%
 # add_header_above(c(" ", "Scores" = 5, "Ranks" = 3," "))

table_export  
```

### DH  
WAR Ranks are for the position, and Pos_Rank is based on Model Score      
```{r}

FanGraph_Positions_Batter_All_POS =
FanGraph_Positions_Batter_Total %>%
filter(PositionDH == 1) %>% 
  mutate(
    max_predscore= max(AdjPredict_Score_raw,na.rm = T),
    AdjPredict_Score = ifelse (is.na(AdjPredict_Score_raw),NA,AdjPredict_Score_raw),
    WAR_rank = order(order(rank(WAR,ties.method = 'average'),decreasing = TRUE)),
    AdjPredict_Score_Rank = order(order(rank(AdjPredict_Score,ties.method = 'average'),decreasing = TRUE))-sum(is.na(AdjPredict_Score)),
        Ranks_Above_ADP = ADP - AdjPredict_Score_Rank
  ) %>%   
  select (Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity)


ft_dt <- FanGraph_Positions_Batter_All_POS[1:nrow(FanGraph_Positions_Batter_All_POS), 1:ncol(FanGraph_Positions_Batter_All_POS)] %>% 
#%>% 
#filter(AdjPredict_Score_Rank>0)
 arrange((ADP))

ft_dt$ADP <- color_tile("white", "red")(ft_dt$ADP)

ft_dt$WAR <- color_bar("lightblue")(ft_dt$WAR)


ft_dt$AdjPredict_Score<-  round((ft_dt$AdjPredict_Score),2)

ft_dt$Model_Score<- color_bar("lightblue")(ft_dt$AdjPredict_Score)

ft_dt$WAR_Rank <- color_tile("green","orange")(ft_dt$WAR_rank)

ft_dt$Position_Rank <- color_tile("green","orange")(ft_dt$AdjPredict_Score_Rank) 


ft_dt$Ranks_Above_ADP <- 
  ifelse(
  ft_dt$Ranks_Above_ADP < 0,
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "red", italic = T),
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "green", italic = T)
)

ft_dt$Positional_Scarcity <- round(ft_dt$Positional_Scarcity, 2)

ft_dt$Pos_Scarcity <- color_tile("red","white")(ft_dt$Positional_Scarcity)

#Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity

ft_dt2 <- ft_dt[c("Name","Team","Position","ADP", "WAR", "Model_Score", "WAR_Rank","Position_Rank","Pos_Scarcity")]


table_export = 
kbl(ft_dt2, escape = F) %>% 
 kable_material(c("striped", "hover","condensed","responsive"),full_width = F,fixed_thead = T) %>%   column_spec(6, width = "3cm")# %>%
 # add_header_above(c(" ", "Scores" = 5, "Ranks" = 3," "))

table_export  
```



## Pitchers by Position {.tabset .tabset-pills}  
My non-normalized model scores for a player are `Model_Score`.    
 
### All P  

```{r}


FanGraph_Positions_Pitcher_Total2 =
pitching_Data_NonAdj_Projections_Total %>% 
  mutate(
    Avg_IP = mean(IP),
    AdjPredict_Score_raw = ifelse(is.na(Predict_Score),NA,Predict_Score*(IP/Avg_IP)*1)
  ) %>% select(-Avg_IP)


FanGraph_Positions_Pitcher_All_POS2 =
FanGraph_Positions_Pitcher_Total2 %>% 
  mutate(
    max_predscore= max(AdjPredict_Score_raw,na.rm = T),
    AdjPredict_Score = ifelse (is.na(AdjPredict_Score_raw),NA,AdjPredict_Score_raw),
    WAR_rank = order(order(rank(WAR,ties.method = 'average'),decreasing = TRUE)),
    AdjPredict_Score_Rank = order(order(rank(AdjPredict_Score,ties.method = 'average'),decreasing = TRUE))-sum(is.na(AdjPredict_Score)),
        Ranks_Above_ADP = ADP - AdjPredict_Score_Rank
  ) %>%
filter(IP > 19) %>%   
  select (Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity)

ft_dt <- FanGraph_Positions_Pitcher_All_POS2[1:nrow(FanGraph_Positions_Pitcher_All_POS2), 1:ncol(FanGraph_Positions_Pitcher_All_POS2)] %>% 
#%>% 
#filter(AdjPredict_Score_Rank>0)
 arrange((ADP))

ft_dt$ADP <- color_tile("white", "red")(ft_dt$ADP)

ft_dt$WAR <- color_bar("lightblue")(ft_dt$WAR)


ft_dt$AdjPredict_Score<-  round((ft_dt$AdjPredict_Score),2)

ft_dt$Model_Score<- color_bar("lightblue")(ft_dt$AdjPredict_Score)

ft_dt$WAR_Rank <- color_tile("green","orange")(ft_dt$WAR_rank)

ft_dt$Pitching_Rank <- color_tile("green","orange")(ft_dt$AdjPredict_Score_Rank) 


ft_dt$Ranks_Above_ADP <- 
  ifelse(
  ft_dt$Ranks_Above_ADP < 0,
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "red", italic = T),
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "green", italic = T)
)

ft_dt$Positional_Scarcity <- round(ft_dt$Positional_Scarcity, 2)

ft_dt$Pos_Scarcity <- color_tile("red","white")(ft_dt$Positional_Scarcity)

#Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity

ft_dt2 <- ft_dt[c("Name","Team","Position","ADP", "WAR", "Model_Score", "WAR_Rank","Pitching_Rank","Pos_Scarcity")]


table_export = 
kbl(ft_dt2, escape = F) %>% 
 kable_material(c("striped", "hover","condensed","responsive"),full_width = F,fixed_thead = T) %>%   column_spec(6, width = "3cm")# %>%
 # add_header_above(c(" ", "Scores" = 5, "Ranks" = 3," "))

table_export  


```


### SP
WAR Ranks are for the position, and Pos_Rank is based on Model Score    
```{r}

FanGraph_Positions_Pitcher_Total2 =
pitching_Data_NonAdj_Projections_Total %>% 
  mutate(
    Avg_IP = mean(IP),
    AdjPredict_Score_raw = ifelse(is.na(Predict_Score),NA,Predict_Score*(IP/Avg_IP)*1)
  ) %>% select(-Avg_IP) %>%
filter(IP > 19 & PositionSP ==1)


FanGraph_Positions_Pitcher_All_POS2 =
FanGraph_Positions_Pitcher_Total2 %>% 
  mutate(
    max_predscore= max(AdjPredict_Score_raw,na.rm = T),
    AdjPredict_Score = ifelse (is.na(AdjPredict_Score_raw),NA,AdjPredict_Score_raw),
    WAR_rank = order(order(rank(WAR,ties.method = 'average'),decreasing = TRUE)),
    AdjPredict_Score_Rank = order(order(rank(AdjPredict_Score,ties.method = 'average'),decreasing = TRUE))-sum(is.na(AdjPredict_Score)),
        Ranks_Above_ADP = ADP - AdjPredict_Score_Rank
  )  %>%   
  select (Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity)

ft_dt <- FanGraph_Positions_Pitcher_All_POS2[1:nrow(FanGraph_Positions_Pitcher_All_POS2), 1:ncol(FanGraph_Positions_Pitcher_All_POS2)] %>% 
#%>% 
#filter(AdjPredict_Score_Rank>0)
 arrange((ADP))

ft_dt$ADP <- color_tile("white", "red")(ft_dt$ADP)

ft_dt$WAR <- color_bar("lightblue")(ft_dt$WAR)


ft_dt$AdjPredict_Score<-  round((ft_dt$AdjPredict_Score),2)

ft_dt$Model_Score<- color_bar("lightblue")(ft_dt$AdjPredict_Score)

ft_dt$WAR_Rank <- color_tile("green","orange")(ft_dt$WAR_rank)

ft_dt$Position_Rank <- color_tile("green","orange")(ft_dt$AdjPredict_Score_Rank) 


ft_dt$Ranks_Above_ADP <- 
  ifelse(
  ft_dt$Ranks_Above_ADP < 0,
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "red", italic = T),
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "green", italic = T)
)

ft_dt$Positional_Scarcity <- round(ft_dt$Positional_Scarcity, 2)

ft_dt$Pos_Scarcity <- color_tile("red","white")(ft_dt$Positional_Scarcity)

#Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity

ft_dt2 <- ft_dt[c("Name","Team","Position","ADP", "WAR", "Model_Score", "WAR_Rank","Position_Rank","Pos_Scarcity")]


table_export = 
kbl(ft_dt2, escape = F) %>% 
 kable_material(c("striped", "hover","condensed","responsive"),full_width = F,fixed_thead = T) %>%   column_spec(6, width = "3cm")# %>%
 # add_header_above(c(" ", "Scores" = 5, "Ranks" = 3," "))

table_export  


```



### RP  
WAR Ranks are for the position, and Pos_Rank is based on Model Score    
```{r}

FanGraph_Positions_Pitcher_Total2 =
pitching_Data_NonAdj_Projections_Total %>% 
  mutate(
    Avg_IP = mean(IP),
    AdjPredict_Score_raw = ifelse(is.na(Predict_Score),NA,Predict_Score*(IP/Avg_IP)*1)
  ) %>% select(-Avg_IP) %>%
filter(IP > 19 & PositionRP ==1)


FanGraph_Positions_Pitcher_All_POS2 =
FanGraph_Positions_Pitcher_Total2 %>% 
  mutate(
    max_predscore= max(AdjPredict_Score_raw,na.rm = T),
    AdjPredict_Score = ifelse (is.na(AdjPredict_Score_raw),NA,AdjPredict_Score_raw),
    WAR_rank = order(order(rank(WAR,ties.method = 'average'),decreasing = TRUE)),
    AdjPredict_Score_Rank = order(order(rank(AdjPredict_Score,ties.method = 'average'),decreasing = TRUE))-sum(is.na(AdjPredict_Score)),
        Ranks_Above_ADP = ADP - AdjPredict_Score_Rank
  )  %>%   
  select (Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity)

ft_dt <- FanGraph_Positions_Pitcher_All_POS2[1:nrow(FanGraph_Positions_Pitcher_All_POS2), 1:ncol(FanGraph_Positions_Pitcher_All_POS2)] %>% 
#%>% 
#filter(AdjPredict_Score_Rank>0)
 arrange((ADP))

ft_dt$ADP <- color_tile("white", "red")(ft_dt$ADP)

ft_dt$WAR <- color_bar("lightblue")(ft_dt$WAR)


ft_dt$AdjPredict_Score<-  round((ft_dt$AdjPredict_Score),2)

ft_dt$Model_Score<- color_bar("lightblue")(ft_dt$AdjPredict_Score)

ft_dt$WAR_Rank <- color_tile("green","orange")(ft_dt$WAR_rank)

ft_dt$Position_Rank <- color_tile("green","orange")(ft_dt$AdjPredict_Score_Rank) 


ft_dt$Ranks_Above_ADP <- 
  ifelse(
  ft_dt$Ranks_Above_ADP < 0,
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "red", italic = T),
  cell_spec(round(ft_dt$Ranks_Above_ADP,2), color = "green", italic = T)
)

ft_dt$Positional_Scarcity <- round(ft_dt$Positional_Scarcity, 2)

ft_dt$Pos_Scarcity <- color_tile("red","white")(ft_dt$Positional_Scarcity)

#Name,Team,Position,ADP,WAR, WAR_rank,AdjPredict_Score ,AdjPredict_Score_Rank,Ranks_Above_ADP, Positional_Scarcity

ft_dt2 <- ft_dt[c("Name","Team","Position","ADP", "WAR", "Model_Score", "WAR_Rank","Position_Rank","Pos_Scarcity")]


table_export = 
kbl(ft_dt2, escape = F) %>% 
 kable_material(c("striped", "hover","condensed","responsive"),full_width = F,fixed_thead = T) %>%   column_spec(6, width = "3cm")# %>%
 # add_header_above(c(" ", "Scores" = 5, "Ranks" = 3," "))

table_export  


```



